/**
 * Created by c.k on 2017/11/20.
 *  global WebViewJavascriptBridge
* */
import deviceInfo from './deviceInfo'
// import store from '../store'
// import statisticHelper from './statisticalHelper'
// import channelHelper from './channel'
const setupWebViewJavascriptBridge = callback => {
  // 注册事件监听
  if (window.WebViewJavascriptBridge) {
    return callback(WebViewJavascriptBridge)
  } else {
    document.addEventListener('WebViewJavascriptBridgeReady', () => {
      callback(WebViewJavascriptBridge)
    }, false)
  }

  if (window.WVJBCallbacks) {
    return window.WVJBCallbacks.push(callback)
  }
  window.WVJBCallbacks = [callback];
  let WVJBIframe = document.createElement('iframe');
  WVJBIframe.style.display = 'none';
  WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
  document.documentElement.appendChild(WVJBIframe);
  setTimeout(function () {
    document.documentElement.removeChild(WVJBIframe)
  }, 0)
};

export default {
  showLog(log) {
    setupWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('showLog', log, function responseCallback(responseData) {
      })
    })
  },
  // 获取原生设备信息
  fetchDeviceInfo() {
    if (deviceInfo.getDeviceType() === 'Android') {
      if (window.android && window.android.fetchDeviceInfo) {
        let responseData = window.android.fetchDeviceInfo();
        deviceInfo.saveDeviceInfo(responseData);
      }
    } else {
      setupWebViewJavascriptBridge(function (bridge) {
        bridge.callHandler('kJSFetchDeviceInfo', function (responseData) {
          deviceInfo.saveDeviceInfo(responseData);
        })
      })
    }
  },
  // 回传页面链接至原生
  sendApplyPageUrlToNative() {
    setupWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('kJSFetchApplyURL', window.location.href, function responseCallback(responseData) {
      })
    })
  },
  // 回传页面信息至原生
  sendApplyPageInfoToNative(info) {
    setupWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('kJSFetchProductInfo', info, function responseCallback(responseData) {
      })
    })
  },
  // 每次启动app时清除之前的登录状态
  logoutWhenAppLaunch() {
    // for android
    // window.forceLogoutWhenAppLaunch = function () {
    //   store.dispatch('logout')
    //   // 首次进入app，默认统计第一个按钮点击事件
    //   statisticHelper.tabbarEventStatistical('loansupermarket');
    // }
    // for iOS
    // setupWebViewJavascriptBridge(function (bridge) {
    //   bridge.registerHandler('kJSAppLaunch', function (data) {
    //     store.dispatch('logout')
    //     // 首次进入app，默认统计第一个按钮点击事件
    //     statisticHelper.tabbarEventStatistical('loansupermarket');
    //   })
    // })
    // if (!channelHelper.isApp()) {
    //   statisticHelper.tabbarEventStatistical('loansupermarket');
    // }
  },
  // 隐藏原生启动页
  hideNativeLaunchImage() {
    // for android
    if (window.appNative && window.appNative.hideLaunchView) {
      window.appNative.hideLaunchView()
    }
    // for iOS
    setupWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('kJSHideLaunchImage', function responseCallback(responseData) {
      })
    })
  },
  // 隐藏显示原生header
  showAndHideNativeHeader(show) {
    // for android
    if (window.native && window.native.showHeader) {
      window.native.showHeader(show)
    }
    // for iOS
    setupWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('kJSShowAndHideNativeHeader', show, function responseCallback(responseData) {
      })
    })
  },
  // 打开微信
  openWX() {
    // for android
    if (window.android && window.android.openWX) {
      window.android.openWX()
    }
    // for iOS
    // setupWebViewJavascriptBridge(function (bridge) {
    //   bridge.callHandler('kJSOpenWeixin', function responseCallback(responseData) {
    //   })
    // })
  },
    // 打开QQ
    openQQ() {
        // for android
        if (window.android && window.android.openQQApplication) {
            window.android.openQQApplication()
        }
        // for iOS
        // setupWebViewJavascriptBridge(function (bridge) {
        //     bridge.callHandler('kJSOpenWeixin', function responseCallback(responseData) {
        //     })
        // })
    },
    //打开通讯录
    openConect(callback){
        console.log("js打开通讯录");
        //for iOS
        setupWebViewJavascriptBridge(function (bridge) {

            bridge.callHandler('kJSOpenConect', function responseCallback(responseData) {


            })

             bridge.registerHandler('sendContectInfo',function (data,responseData) {
              
                
                if(callback){
                      callback(data);
                }

                 

            })

        })

    },
    //获取高德地图定位位置信息
    getAMMapLoactionMsg(callback){

      console.log("js获取位置信息");
        setupWebViewJavascriptBridge(function (bridge) {

            bridge.callHandler('kJSGetAMMapLoactionInfo', function responseCallback(responseData) {

                   // alert(responseData.detail);

                   if(callback){
                      callback(responseData);
                }

            })

            // bridge.registerHandler('sendAMMapLoactionInfo',function (data,responseData) {
              
            //     // alert(data.address);
               
            //     if(callback){
            //           callback(data);
            //     }


            // })

        })

    },

    //调用ios相机
    openCamera(callback){


        setupWebViewJavascriptBridge(function (bridge) {

            bridge.callHandler('kJSGetCameraInfo', function responseCallback(responseData) {

                if(callback){
                      callback(responseData);
                }
  
             })
          })

    },

    openAlbum(callback){

        setupWebViewJavascriptBridge(function (bridge) {

            bridge.callHandler('kJSGetAlbumInfo', function responseCallback(responseData) {

                if(callback){
                  
                      callback(responseData);
                }
  
             })
          })

    },


    
  // 检查定位权限是否打开
  // checkUserAuthorization(callback) {
  //   // for android
  //   if (deviceInfo.isAndroidDevice()) {
  //     if (window.native && window.native.checkUserAuthorization) {
  //       let isOpen = window.native.checkUserAuthorization();
  //       if (isOpen && isOpen === 'true') {
  //         callback(true);
  //       } else {
  //         callback(false);
  //       }
  //     } else {
  //       callback(true);
  //     }
  //   }
  //   // for iOS
  //   setupWebViewJavascriptBridge(function (bridge) {
  //     bridge.callHandler('kJSCheckUserAuthorization', function responseCallback(responseData) {
  //       if (responseData && responseData === 'true') {
  //         callback(true);
  //       } else {
  //         callback(false);
  //       }
  //     })
  //   })
  // }
}
